module.exports = {
    extend: function(target, source, flag) {
        for(var key in source) {
            if(source.hasOwnProperty(key))
                flag ?
                    (target[key] = source[key]) :
                    (target[key] === void 0 && (target[key] = source[key]));
        }
        return target;
    },
    jsonWrite:function(res,ret){
    	if(typeof ret === 'undefined'){
			res.json({
				code:'1',
				msg:'操作失败'
			})
		}else{
			res.json(ret);
		}
    },
    https:function(url,method,data){
        method = method || 'GET';
        var Q = require('q');
        var deferred = Q.defer();
        var https = require("https");  
        var iconv = require("iconv-lite");  
        // var url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+wxApiInfo.appId+"&secret="+wxApiInfo.secret+"&code="+code+"&grant_type=authorization_code";  
        https.get(url, function (_res) {  
            var datas = [];  
            var size = 0;  
            _res.on('data', function (data) {  
                datas.push(data);  
                size += data.length;  
                //process.stdout.write(data);  
            });  
            _res.on("end", function () {  
                var buff = Buffer.concat(datas, size);  
                // var result = iconv.decode(buff, "utf8");//转码
                var result = JSON.parse(buff.toString());//不需要转编码,直接tostring  
                // console.log(result); 
                deferred.resolve(result);
            });  
        }).on("error", function (err) {  
            console.log(err.stack) 
            deferred.reject(err);
        }); 

        return deferred.promise;
    }
}